/*
    Android UIML

	Copyright (C) 2010  Bram Goffings (bramgoffings@gmail.com)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public License
	as published by the Free Software Foundation; either version 2.1
	of	the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

package uiml.android.rendering;

import java.util.ArrayList;

import uiml.android.elements.Property;
import uiml.android.elements.peers.DMethod;
import uiml.android.elements.peers.DParam;
import uiml.android.elements.peers.DProperty;

public class ReflectionParamTools {

	/**
	 * 
	 * @param dproperty
	 * @return
	 */
	public static Class<?>[] getArgumentTypes(DProperty dproperty) {
		return getArgumentTypesFromDParams(dproperty.getDParams());
	}
	
	/**
	 * 
	 * @param dproperty
	 * @return
	 */
	public static Class<?>[] getArgumentTypes(DMethod dmethod) {
		return getArgumentTypesFromDParams(dmethod.getDParams());
	}
	
	/**
	 * 
	 * @param dparams
	 * @return
	 */
	private static Class<?>[] getArgumentTypesFromDParams(ArrayList<DParam> dparams) {
		if(dparams.size() == 0) {
			Class<?> argumentTypes[] = {};
			return argumentTypes;
		}
		else {
			
			Class<?> classArray[] = new Class[dparams.size()];
			
			for(int i=0; i<dparams.size(); ++i)
				classArray[i] = TypeDecoder.getInstance(new AndroidTypeDecoder()).getClassOf(dparams.get(i).getType());	
			
			return classArray;				
		}	
	}
	
	/**
	 * 
	 * @param property
	 * @return
	 */
	public static Object[] getArguments(Property property, Class<?>[] argTypes) {

		if(property.getValue()!=null) {
			
			Object[] arguments = TypeDecoder.getInstance(new AndroidTypeDecoder()).getObjectOf(property, argTypes);
			return arguments;
		}
		else {
			
			Object arguments[] = {};
			return arguments;
		}
	}
	
}
